/** ~ CopyRight © 2012 USTC SINOVATE  SOFTWARE CO.LTD All Rights Reserved. */
package com.cmcc.zysoft.infocollection.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.dto.LoginOutLogDto;
import com.cmcc.zysoft.infocollection.service.LoginOutLogPcService;
import com.cmcc.zysoft.infocollection.util.ExportExcel;
import com.cmcc.zysoft.infocollection.util.TransCodingToUtf8;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: LoginOutLogController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-13 下午2:33:01
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/pc/loginLog")
public class LoginOutLogController extends BaseController {
	
	/**
	 * 属性名称: loginOutLogPcService.
	 * 类       型: LoginOutLogPcService.
	 */
	@Resource
	private LoginOutLogPcService loginOutLogPcService;
	
	/**
	 * 跳转到登录日志查询页面.
	 * @return String
	 */
	@RequestMapping(value="/main.htm")
	public String main(){
		return "/loginLog/loginLog";
	}
	
	/**
	 * 登录日志记录 可根据登录时间查询.
	 * @param page 
	 * @param rows 
	 * @param startTime 
	 * @param endTime 
	 * @return map
	 */
	@RequestMapping(value="/list.htm")
	@ResponseBody
	public Map<String, Object> list(int page,int rows,
			@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
		return this.loginOutLogPcService.list(rows, page, startTime, endTime);
	}
	
	/**
	 * 登陆日志导出.
	 * @param startTime 
	 * @param endTime 
	 * @param response 
	 * @return map
	 * @throws ParseException 
	 */
	@RequestMapping(value="/export.htm")
	@ResponseBody
	public Map<String, Object> exportList(String startTime,String endTime,
			HttpServletResponse response) throws ParseException{
		String[] headers = {"用户名", "操作类型", "操作时间", "登陆IP", "客户端类型", "登录结果", "登陆日志", "登陆耗时"};
		String title = "loginLog.xls"; //报表名称
		String pattern = "yyyy-MM-dd hh:mm:ss";
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			OutputStream out = response.getOutputStream();
			response.reset();//清空输出流  
			//设定输出文件头  
			response.setHeader("Content-disposition", "attachment; filename=" + TransCodingToUtf8.toUtf8String(title));
			response.setContentType("application/ms-excel;charset=UTF-8"); //定义输出类型
			List<LoginOutLogDto> exportList = this.loginOutLogPcService.getListForExcel(startTime, endTime);
			ExportExcel<LoginOutLogDto>  exportExcel = new ExportExcel<LoginOutLogDto>();
			exportExcel.exportExcel(title, headers, exportList, out, pattern);
		} catch (IOException e) {
			e.printStackTrace();
			result.put("msg", "报表导出失败,请稍候重试!");
		}
		result.put("msg", "报表导出成功");
		return result;
	}
	
	/**
	 * 删除日志.
	 * @param logIds 
	 * @return  String
	 */
	@RequestMapping(value="/delete.htm")
	@Transactional
	@ResponseBody
	public String delete(String logIds){
		String[] ids = logIds.split(",");
		for(int i=0;i<ids.length;i++){
			this.loginOutLogPcService.deleteById(ids[i]);
		}
		return "SUCCESS";
	}
}